﻿using System;
using System.Collections.Generic;
using System.Text;
using UIShell.OSGi;
using System.Windows.Forms;
using UIShell.OSGi.Core.Service;
using Pasys.PresentationCore;
using Pasys.PresentationCore.WinForms;
using Pasys.PresentationCore.Impl;
using UIShell.MessageBusService;
using Pasys.PresentationCore.DockShell;

namespace WinFormShell
{
    public class Activator : IBundleActivator
    {
        //private IExtensionManager _extensionManager;
        private ExtensionHooker _extensionHooker;

        public void Start(IBundleContext context)
        {
            var im = context.GetFirstOrDefaultService<IMessageBusService>();
            context.AddService<ICommandBusService>(new CommandBusService(im));

            var layoutView = new ShellDockLayoutView();
            //context.AddService<IWorkspace>(layoutView.Workspace);

            var form = new RootForm();
            BundleRuntime.Instance.AddService<ILayoutViewProxyContainer>(form);
            context.AddService<Form>(form);
            form.ShowLayoutIvew(layoutView);

            var layoutViewProxy = layoutView.LayoutViewProxy;
            context.AddService(layoutViewProxy);

            var _extensionManager = context.GetFirstOrDefaultService<IExtensionManager>();
            _extensionHooker = new ExtensionHooker(_extensionManager);

            var extensionChangeHandler = new ExtensionChangeHandler(new ToolStripBuilder(),
                                                        toolStrip =>
                                                        layoutViewProxy.AddToolStrip((ToolStrip)toolStrip),
                                                        toolStrip =>
                                                        layoutViewProxy.RemoveToolStrip((ToolStrip)toolStrip));

            _extensionHooker.HookExtension(ExtensionPointNames.ToolStrip, extensionChangeHandler);

            extensionChangeHandler = new ExtensionChangeHandler(new MainMenuBuilder(),
                                          menuCreator => {
                                               MenuCreator mc = (MenuCreator)menuCreator;
                                               if (mc.Index < 0)
                                               {
                                                   layoutViewProxy.AddMenuItem(mc.Item);
                                               }
                                               else
                                               {
                                                   layoutViewProxy.InsertMenuItem(mc.Index,mc.Item);
                                               }
                                          },
                                          toolStripItem => layoutViewProxy.RemoveMenuItem((ToolStripItem)toolStripItem));
            _extensionHooker.HookExtension(ExtensionPointNames.MainMenu, extensionChangeHandler);


            var commandBus = context.GetFirstOrDefaultService<ICommandBusService>();
            commandBus.SubscribeCommandEvent(CommandNames.LoadLayout, (s, e) => {
                layoutView.Workspace.LoadLayout("");
            });

            commandBus.SubscribeCommandEvent(CommandNames.SaveLayout, (s, e) =>
            {
                layoutView.Workspace.SaveLayout("");
            });


        }

        public void Stop(IBundleContext context)
        {
            _extensionHooker.Dispose();
        }
    }


}
